package com.hunliji.monitor.web.facade.impl;

import com.alibaba.fastjson.JSON;
import com.hunliji.monitor.service.api.AlertConfigService;
import com.hunliji.monitor.service.api.model.bo.AlertConfigBO;
import com.hunliji.monitor.web.facade.AliyunNotifyFacade;
import com.hunliji.monitor.web.model.param.AliyunAlertNotifyParam;
import com.hunliji.monitor.web.strategy.alert.AlertActionClient;
import com.hunliji.monitor.web.strategy.alert.model.AlertActionResultBO;
import com.hunliji.monitor.web.strategy.alert.model.AlertContext;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

/**
 * File：AliyunNotifyFacadeImpl.java<br> Title: <br> Description: <br>
 *
 * @author lejin Date: 2022/10/10
 */
@Service
@Slf4j
public class AliyunNotifyFacadeImpl implements AliyunNotifyFacade {

    @Resource
    AlertConfigService alertConfigService;

    @Override
    public void alert(List<AliyunAlertNotifyParam> param) {
        if(CollectionUtils.isEmpty(param)){
            return;
        }
        param.forEach(this::alert);
    }

    public void alert(AliyunAlertNotifyParam param){
        if(Objects.isNull(param)){
            return;
        }
        String alertId = param.getAlertId();
        String project = param.getProject();
        if(StringUtils.isBlank(alertId) || StringUtils.isBlank(project)){
            log.warn("阿里云请求体参数缺少关键参数，alertId:{} project: {}", alertId, project);
            return;
        }
        AlertConfigBO alertConfigBO = alertConfigService.getByQuery(
            AlertConfigBO.builder().alertId(alertId).project(project).build());
        if(Objects.isNull(alertConfigBO)){
            alertConfigBO = AlertConfigBO.builder()
                .alertId(alertId)
                .project(project)
                .alertName(param.getAlertName())
                .actionTypes("")
                .minActionSeverity(0)
                .build();
            log.info("发现接入新告警，向数据库插入新数据，{}", JSON.toJSONString(alertConfigBO));
            alertConfigService.insertOne(alertConfigBO);
        } else {
            alertConfigBO.setAlertName(param.getAlertName());
            alertConfigService.updateOne(alertConfigBO);
        }
        if(!alertConfigBO.getEnableStatus()){
            log.info("此告警未开启，不做处理{}", JSON.toJSONString(alertConfigBO));
            return;
        }
        if(Objects.nonNull(param.getSeverity()) && param.getSeverity() < alertConfigBO.getMinActionSeverity()){
            log.info("未达到最小触发行动告警级别{}，不处理", alertConfigBO.getMinActionSeverity());
            return;
        }
        AlertContext alertContext = AlertContext.builder().alertConfigId(alertConfigBO.getId()).aliyunAlertNotifyParam(param).build();
        Map<String, AlertActionResultBO> actionResultBOMap = AlertActionClient.actions(alertContext,
            alertConfigBO.getActionTypes());
        if(Objects.nonNull(actionResultBOMap)){
            for (Entry<String, AlertActionResultBO> entry : actionResultBOMap.entrySet()) {
                String actionType = entry.getKey();
                AlertActionResultBO alertActionResultBO = entry.getValue();
                if(!alertActionResultBO.getSuccess()){
                    log.warn("处理器actionType：{} 处理失败，错误原因: {}", actionType, alertActionResultBO.getErrorMsg());
                }
            }
        }
    }

}
